package cn.akwangl.java8.test.lambda;

import cn.akwangl.database.model.Person;
import org.junit.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 1. 方法引用 {@link MethodReferences#MethodReferencesTest()}
 * 2. 构造器引用 {@link MethodReferences#constructorTest()}
 * 3. 数组引用 {@link MethodReferences#arrayTest()} ()}
 * <p>
 * tips:在实际编写过程中是对"类型推断"的加深
 *
 * @author youyongkun
 * @date 2021/5/28 13:46
 */
public class MethodReferences {

    /**
     * 方法引用
     * 1. 静态方法
     * 2. 类::实例方法引用
     * 3. 对象::实例方法引用
     *
     * @author youyongkun
     * @date 2021/5/28 14:20
     */
    @Test
    public void MethodReferencesTest() {
        /**
         * 静态方法引用
         */
        Supplier<LocalDateTime> now = LocalDateTime::now;
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        /**
         * 类::实例方法引用
         * dtf.format(LocalDateTime.now());
         */
        BiFunction<DateTimeFormatter, TemporalAccessor, String> biFunction = DateTimeFormatter::format;
        String dataTimeStr = biFunction.apply(dtf, now.get());
        System.out.println(dataTimeStr);

        /**
         * 对象::实例方法引用
         */
        Supplier<String> supplier = dtf::toString;
        System.out.println(supplier.get());
    }

    /**
     * 构造器引用
     *
     * @author youyongkun
     * @date 2021/5/28 13:53
     */
    @Test
    public void constructorTest() {
        /**
         * 一个参数构造器的引用
         */
        Function<String, Person> function = Person::new;
        Person person1 = function.apply("1764441920@qq.com");

        /**
         * 两个参数构造器的引用
         */
        BiFunction<String, String, Person> biFunction = Person::new;
        Person person2 = biFunction.apply("@阿坤同桌", "15");
    }


    /**
     * 数组引用
     *
     * @author youyongkun
     * @date 2021/5/28 13:53
     */
    @Test
    public void arrayTest() {
        Function<Integer, String[]> function = String[]::new;
        String[] array = function.apply(16);
    }

}
